package com.yuyou.weixin.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yuyou.weixin.entity.SrsUser;
import com.yuyou.weixin.mapper.SrsUserMapper;
import com.yuyou.weixin.service.ISrsUserService;
import com.yuyou.weixin.util.Sequence;

@Service
public class SrsUserServiceImpl implements ISrsUserService{

	@Autowired
	private SrsUserMapper srsUserMapper;

	@Override
	public int deleteByPrimaryKey(Long userId) {
		return srsUserMapper.deleteByPrimaryKey(userId);
	}

	@Override
	public int insert(SrsUser record) {		
		record.setUserId(Sequence.getSequence().nextId());
		record.setCreateTime(new Date());
		return srsUserMapper.insert(record);
	}

	@Override
	public SrsUser selectByPrimaryKey(Long userId) {
		return srsUserMapper.selectByPrimaryKey(userId);
	}

	@Override
	public int updateByPrimaryKeySelective(SrsUser record) {
		return srsUserMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public SrsUser selectByOpenId(String openId,String projectCode) {
		return srsUserMapper.selectByOpenId(openId,projectCode);
	}

	@Override
	@Transactional
	public void addOrUpUser(String openId,String headImg,String name,String projectCode) {	
		SrsUser srsUser=selectByOpenId(openId,projectCode);
		if(srsUser==null){
			srsUser=new SrsUser();
			srsUser.setOpenId(openId);
			srsUser.setName(name);
			srsUser.setHeadImg(headImg);
			srsUser.setProjectCode(projectCode);
			this.insert(srsUser);
		}else{
			srsUser.setName(name);
			if(StringUtils.isEmpty(srsUser.getHeadImg())){
				srsUser.setHeadImg(headImg);
			}
			else if(!StringUtils.isEmpty(headImg)){
				srsUser.setHeadImg(headImg);
			}
			
			this.updateByPrimaryKeySelective(srsUser);
		}
	}

	@Override
	public List<SrsUser> findImgByActivityId(Long srsActivityId) {
		return srsUserMapper.findImgByActivityId(srsActivityId);
	}
	

	
	

}